package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private DamageListDao damageListDao;
    /**
     * 保存报损单
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        if (user!=null) {
            damageList.setUserId(user.getUserId());
        }
        damageListDao.save(damageList);
        List<DamageListGoods> damageListGoodsList = JSON.parseArray(damageListGoodsStr, DamageListGoods.class);
        for (DamageListGoods damageListGoods : damageListGoodsList) {
//            DamageList damageList1 = damageListDao.selectDamageList(damageList.getDamageNumber());
            damageListGoods.setDamageListId(damageList.getDamageListId());
            damageListGoodsDao.save(damageListGoods);
        }
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 报损单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String sTime, String eTime) {

        Page<DamageList> damageListList = damageListDao.list(sTime,eTime);
        for (DamageList damageList : damageListList) {
            User user = damageListDao.getUser(damageList.getUserId());
            if (user!=null){
                damageList.setTrueName(user.getTrueName());
            }else {
                damageList.setTrueName("null");
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows",damageListList);
        return map;
    }

    /**
     * 查询报损单商品信息
     * @param damageListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        List<DamageListGoods> damageListGoods = damageListGoodsDao.goodList(damageListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",damageListGoods);
        return map;
    }
}
